﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using NENAData.Factory;
using NENAData.LINQ;
using NENAData.Payments;
using NENAData.Transactions;

namespace NENAData.NENAProvinces
{
    public class NENAAffiliate
    {
        
        private static MembershipDataContext GetMembershipDB()
        {
            var x = new SQLMembership();
            return x.GetMembershipDB();
        }

        public Affiliate GetAffiliate_byAffiliateID(int? intAffiliate)
        {

            MembershipDataContext db = GetMembershipDB();
            var listAffilates = (from t1 in db.Affiliates
                                 where t1.Affiliate_ID == intAffiliate
                                 select t1).Single();

            return listAffilates;
        }

        public List<Affiliate> GetAffiliates_byProvinceID(int intProvinceID)
        {

            MembershipDataContext db = GetMembershipDB();
            var listAffilates = (from t1 in db.Affiliates
                                 where t1.Active
                                 select t1);

            return listAffilates.ToList();
        }

        public Affiliate GetAffiliate_byMemberID(int intMemberID)
        {
            MembershipDataContext db = GetMembershipDB();
            var listAffilates = (from a in db.Affiliates
                                 join m in db.Members on a.Affiliate_ID equals m.Affiliate_ID
                                 where m.Member_ID == intMemberID
                                 select a).Single();

            return listAffilates;
        }

        public Affiliate GetCurrentAffiliate_byTransactionID_byMemberID(int intTransactionID, int intMemberID)
        {
            NENATransactions nt = new NENATransactions();
            Transaction trans;
            trans = nt.GetTransaction_byTransactionID(intTransactionID);

            Affiliate oAffiliate;
            switch (trans.TransactionType)
            {
                case "Membership":
                    oAffiliate = GetAffiliate_byMemberID(intMemberID);
                    break;

                default:
                    oAffiliate= new Affiliate();
                    break;

            }

            return oAffiliate;
            
        }

        public Affiliate GetAffiliateFromTransactionPayment(int intTransactionPaymentID)
        {
            NENAPayments ntp = new NENAPayments();
            TransactionPayment tp = ntp.GetTransactionPayment(intTransactionPaymentID);

            int? intAffiliateID = tp.PaymentTo;

            return GetAffiliate_byAffiliateID(intAffiliateID);

        }

        public Affiliate GetAffiliateFromTransactionPayment(int strTransactionID_NENA, int intMemberID)
        {
            NENAPayments ntp = new NENAPayments();
            TransactionPayment tp = ntp.GetTransactionPayment(strTransactionID_NENA, intMemberID);

            int? intAffiliateID = tp.PaymentTo;

            return GetAffiliate_byAffiliateID(intAffiliateID);

        }

        public List<Affiliate>  GetAffiliates_AllActive()
        {

            MembershipDataContext db = GetMembershipDB();
            var listAffilates = (from t1 in db.Affiliates
                                 where t1.Active
                                 select t1);
            
            return listAffilates.ToList();
        }

    }
}